1370B - GCD Compression - CodeForces Solution


constructive algorithms math number theory *1100

Please click on ads to support us..

Python Code:

for pratyush in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    o,e=0,0
    a,b=[],[]
    for i in range(2*n):
        if l[i]%2==0:
            b.append(i)
            e+=1
        else:
            o+=1
            a.append(i)
    if o%2==1:
        a.pop()
        b.pop()
    else:
        if o>1:
            a.pop()
            a.pop()
            o-=2
        else:
            b.pop()
            b.pop()
            e-=2
    for i in range(0,len(a),2):
        print(a[i]+1,a[i+1]+1)
    for i in range(0,len(b),2):
        print(b[i]+1,b[i+1]+1)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n,cnt;
    cin >> n;
    int a[2 * n];
    cnt=0;

    vector<int> even; // carry index
    vector<int> odd;

    for (int i = 0; i < 2 * n; i++)
    {
        cin >> a[i];
        if (a[i] % 2 == 0)
        {
            even.push_back(i + 1);
        }
        else
        {
            odd.push_back(i + 1);
        }
    }

    


    for (int i = 0; i < even.size(); i += 2)
    {
        if (i + 1 < even.size() and cnt<n-1)
        {
            // cout<<"before";
            // cout<<cnt<<"\n";
            cout << even[i] << " " << even[i + 1] << "\n";
            cnt++;
            // cout<<cnt<<endl;
            // cout<<"after";
            
        }
    }

    for (int i = 0; i < odd.size(); i += 2)
    {
        if (i + 1 < odd.size() and cnt<n-1)
        {
        //    cout<<"before";
        //     cout<<cnt<<"\n";
            cout << odd[i] << " " << odd[i + 1] << "\n";
             cnt++;

            //  cout<<cnt<<"\n";
            //  cout<<"after";
            
        }
    }

//     cout<<cnt<<endl;
 }

int main()
{
    int t;
    cin >> t;
    // t = 1;

    while (t--)
    {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols